package cn.edu.sgu.www.mhxysy.controller.partner;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.partner.PartnerDTO;
import cn.edu.sgu.www.mhxysy.entity.partner.Partner;
import cn.edu.sgu.www.mhxysy.pager.partner.PartnerPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.partner.PartnerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "助战管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/partner", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class PartnerController {

    private final PartnerService partnerService;

    @Autowired
    public PartnerController(PartnerService partnerService) {
        this.partnerService = partnerService;
    }

    @ApiOperation("上传图片")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public JsonResult<String> upload(MultipartFile file) throws IOException {
        String url = partnerService.upload(file);

        return JsonResult.success(CustomMessages.UPLOAD_SUCCESS, url);
    }

    @ApiOperation("添加助战")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated PartnerDTO partnerDTO) {
        partnerService.insert(partnerDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID删除助战")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "助战ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        partnerService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改助战信息")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated PartnerDTO partnerDTO) {
        partnerService.updateById(partnerDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID修改助战图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "助战ID", required = true),
            @ApiImplicitParam(name = "image", value = "图片地址", required = true)
    })
    @RequestMapping(value = "/updateImageById", method = RequestMethod.POST)
    public JsonResult<Void> updateImageById(
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id,
            @NotBlank(message = CustomMessages.IMAGE_NOT_NULL) String image) {
        partnerService.updateImageById(id, image);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过助战类型查询助战列表")
    @RequestMapping(value = "/selectByType", method = RequestMethod.GET)
    public JsonResult<List<Partner>> selectByType(
            @ApiParam(name = "type", value = "助战类型", required = false)
            @RequestParam(required = false) Integer type) {
        List<Partner> list = partnerService.selectByType(type);

        return JsonResult.success(null, list);
    }

    @ApiOperation("分页查询助战列表")
    @RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
    public JsonResult<JsonPage<Partner>> selectByPage(PartnerPager pager) {
        Page<Partner> page = partnerService.selectByPage(pager);

        return JsonResult.restPage(page);
    }

}